#define _CRT_SECURE_NO_WARNINGS
#include"common.h"

//密码遮盖模块

//获取光标的位置x
int wherex()
{
	CONSOLE_SCREEN_BUFFER_INFO pBuffer;
	GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &pBuffer);
	return (pBuffer.dwCursorPosition.X + 1);
}

//获取光标的位置y
int wherey()
{
	CONSOLE_SCREEN_BUFFER_INFO pBuffer;
	GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &pBuffer);
	return (pBuffer.dwCursorPosition.Y + 1);
}

//设置光标的位置
void gotoxy(int x, int y)
{
	COORD c;
	c.X = x - 1;
	c.Y = y - 1;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

//输入密码
void enterPad(char ch[])
{
	int i, j;
	int x, y;
	ch[20];
	
	i = -1;
	do
	{
		i++;
		ch[i] = _getch();  //输入密码。
		while (ch[i] == 8 && i >= 0) //删除键时将前面的*号用空白掩盖。
		{
			i--;
			if (i >= 0)
			{
				x = wherex();
				y = wherey();
				gotoxy(x - 1, y);
				printf(" ");
				x = wherex();
				y = wherey();
				gotoxy(x - 1, y);
			}
			ch[i] = _getch();
		}
		if (i >= 0 && ch[i] != 13 && ch[i] != 8)
		{
			x = wherex();
			y = wherey();
			gotoxy(x, y);
			printf("*");   //将输入的密码位置用*号掩盖。

		}

	} while (ch[i] != 13);  //当输入回车键时退出输入。
	ch[i] = '\0';

	
}

//假等待
void wait() {
	printf("\n");
	for (int k = 0; k < 10; k++) {
		Sleep(200);
		printf("*");
	}

}

//密码加密
void encode(char* p) {
	/*int len = strlen(p);
	for (int i = 0; i < len; i++) {
		if (isalpha(p[i])) {
			if (isupper(p[i]))
				p[i] = tolower(p[i]);
			else
				p[i] = toupper(p[i]);
			if ('z' != tolower(p[i]))
				p[i]++;
			else
			{
				if (isupper(p[i]))
					p[i] = 'A';
				else
					p[i] = 'a';
			}
		}
		else if (isdigit(p[i]))
		{
			if ('9' != p[i])
				p[i]++;
			else
				p[i] = '0';
		}
	}*/
	int hash = 0;
	while (*p != '\0') {
		hash = (hash << 5) + *p++;
	}
	char pp[20];  

	
	for (int i = 0; i < 5; i++) {
		pp[i] = (char)(hash % 10);  
		hash /= 10;  
	}
	p = pp;


}

//密码解密
void decode(char* p) {
	int len = strlen(p);
	for (int i = 0; i < len; i++) {
		if (isalpha(p[i])) {
			if (isupper(p[i]))
				p[i] = tolower(p[i]);
			else
				p[i] = toupper(p[i]);
			if ('a' != tolower(p[i]))
				p[i]--;
			else {
				if (isupper(p[i]))
					p[i] = 'Z';
				else
					p[i] = 'z';
			}
		}
		else if (isdigit(p[i])) {
			if ('0' != p[i])
				p[i]--;
			else
				p[i] = '9';
		}
	}
}